package club.zhaoliang.study.leakcanary;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.squareup.leakcanary.RefWatcher;

import club.zhaoliang.study.R;
import club.zhaoliang.study.app.DemoApplication;

public class LeakCanaryActivity extends AppCompatActivity {

    private LeakThread leakThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_canary);

        leakThread = new LeakThread();
        leakThread.start();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = DemoApplication.getRefWatcher(this);
        //观察当前activity
        refWatcher.watch(this);

        /**
         * 泄漏原因
         LeakCanaryActivity 存在内存泄漏，原因就是非静态内部类LeakThread持有外部类 LeakCanaryActivity 的引用，
         LeakThread中做了耗时操作，导致 LeakCanaryActivity 无法被释放

         解决办法
         将 LeakThread 设置成 静态内部类
         **/


    }

    //    static class LeakThread extends Thread {
    class LeakThread extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(6 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
